package com.uye.framework.model;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * Applies additional initialization logic every time an instance of the Target
 * is constructed.
 * 
 * @author uye
 * @param <T>
 * @param <PK>
 */
public class PostProcessorDAODecorator<T, PK extends Serializable> extends
        DAODecorator<T, PK> {

    /**
     * Initializes and wraps the decoratedDAO.
     * 
     * @param decoratedDao
     */
    public PostProcessorDAODecorator(IGenericDAO<T, PK> decoratedDao) {
        super(decoratedDao);
    }

    public T executePostProcessor(T obj) {
        return obj;
    }

    /**
     * Executes the post processor to all the objects in the collection.
     * 
     * @param objs
     * @return the original collection, but with updated members
     */
    public final Collection<T> executeObjectPostProcessor(Collection<T> objs) {
        for (T t : objs) {
            executePostProcessor(t);
        }
        return objs;
    }

    public T get(PK id) {
        return executePostProcessor(getDecoratedObject().get(id));
    }

    @Override
    public List<T> getAll() {
        return (List<T>) executeObjectPostProcessor(getDecoratedObject().getAll());
    }

    @Override
    public List<T> getAllDistinct() {
        return (List<T>) executeObjectPostProcessor(getDecoratedObject()
                .getAllDistinct());
    }

    @Override
    public boolean exists(PK id) {
        return getDecoratedObject().exists(id);
    }

    @Override
    public T save(T object) {
        return getDecoratedObject().save(object);
    }

    @Override
    public void remove(PK id) {
        getDecoratedObject().remove(id);
    }

}
